package net.lcheng.shop.Controller;

import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.WxShop;
import net.lcheng.model.WxShopCategory;
import net.lcheng.service.WxShopCategoryService;
import net.lcheng.service.WxShopService;
import net.lcheng.shop.vo.CurrentUserModel;
import net.lcheng.vo.WxShopCategoryModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by zwr on 2016/7/19 0019.
 */
@Controller
public class WxShopCategoryController {
    @Autowired
    private WxShopCategoryService wxShopCategoryService;
    @Autowired
    private WxShopService wxShopService;
    /**
     * 店铺管理
     */
    @RequestMapping(value = "/shop/{shopId}/category", method = RequestMethod.GET)
    public String shopCategoryGet(Model model, @PathVariable(value = "shopId") Integer shopId) {
        if (shopId!=null){
            //获取店铺id
            WxShop wxShop = wxShopService.getWxShopByShopId(shopId);
            if (wxShop!=null){
                List<WxShopCategory> wxShopCategoryList = wxShopCategoryService.getWxShopCategoryByShopId(wxShop.getShopId());
                model.addAttribute("wxShopCategoryList", wxShopCategoryList);
            }else{
                return "redirect:/home";
            }
        }else{
            return "redirect:/home";
        }
        return "shop_category";
    }
    /**
     * 添加店铺分类Get
     */
    @RequestMapping(value = "add/category", method = RequestMethod.GET)
    public String addShopCategoryGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        if (currentUser.isAuthenticated()) {
            Session session = currentUser.getSession();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            //获取店铺id
            WxShop wxShop = wxShopService.getShopInforByUserIdAndShopStatus(currentUserModel.getUserId(),2);
            if (wxShop==null){
                return "redirect:/home";
            }
        }else {
            return "redirect:/login";
        }
        model.addAttribute("WxShopCategoryModel", new WxShopCategoryModel());
        return "add_category";
    }
    /**
     * 添加店铺分类Post
     */
    @RequestMapping(value = "add/category", method = RequestMethod.POST)
    public String addShopCategoryPost(Model model, @Valid @ModelAttribute("WxShopCategoryModel") WxShopCategoryModel wxShopCategoryModel, BindingResult result) {
        if (result.hasErrors()){
            return "add_category";
        }
        Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        Session session = currentUser.getSession();
        CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
        WxShop wxShop = wxShopService.getShopInforByUserId(currentUserModel.getUserId());
        WxShopCategory wxShopCategory = new WxShopCategory();
        wxShopCategory.setCategoryId(UUID.randomUUID().toString());//店铺分类id
        wxShopCategory.setCategoryName(wxShopCategoryModel.getCategoryName());//分类名称
        wxShopCategory.setOrderNum(wxShopCategoryModel.getOrderNum());//优先等级
        wxShopCategory.setShopId(wxShop.getShopId());//店铺id
        wxShopCategory.setProductCount(wxShopCategoryModel.getProductCount());//产品数量
        wxShopCategory.setCreateOn(new Date());//创建分类时间
        wxShopCategory.setStatus(1);//分类状态
        int returnResult = wxShopCategoryService.addCategory(wxShopCategory);
        if (returnResult > 0) {
            return "redirect:/shop/category";
        }
        return "redirect:/shop/category";
    }
    /**
     * 编辑店铺分类Get
     */
    @RequestMapping(value = "edit/category", method = RequestMethod.GET)
    public String editShopCategoryGet(Model model, @RequestParam String categoryId) {
        WxShopCategory wxShopCategory = wxShopCategoryService.getWxShopCategoryByCategoryId(categoryId);
        WxShopCategoryModel shopCategoryModel = new WxShopCategoryModel();
        shopCategoryModel.setCategoryId(wxShopCategory.getCategoryId());
        shopCategoryModel.setCategoryName(wxShopCategory.getCategoryName());
        shopCategoryModel.setOrderNum(wxShopCategory.getOrderNum());
        model.addAttribute("WxShopCategoryModel", shopCategoryModel);
        model.addAttribute("wxShopCategory",wxShopCategory);
        return "edit_category";
    }
    /**
     * 编辑店铺分类post
     */
    @RequestMapping(value = "edit/category", method = RequestMethod.POST)
    public String editShopCategoryPost(Model model, @Valid @ModelAttribute("WxShopCategoryModel") WxShopCategoryModel wxShopCategoryModel, BindingResult result) {
        if (result.hasErrors()) {
            return "edit_category";
        }
        WxShopCategory wxShopCategory = new WxShopCategory();
        wxShopCategory.setCategoryId(wxShopCategoryModel.getCategoryId());//店铺分类id
        wxShopCategory.setCategoryName(wxShopCategoryModel.getCategoryName());//分类名称
        wxShopCategory.setOrderNum(wxShopCategoryModel.getOrderNum());//优先等级
        wxShopCategory.setModifyOn(new Date());
        int returnResult = wxShopCategoryService.modifyCategory(wxShopCategory);
        if (returnResult > 0) {
            return "redirect:/shop/category";
        }
        return "redirect:/shop/category";
    }
    /**
     * 删除分类Get(通过修改状态来修改分类)
     */
    @RequestMapping(value = "delete/category", method = RequestMethod.GET)
    public String deleteShopCategoryGet(Model model, @RequestParam String categoryId) {
        WxShopCategory wxShopCategory = new WxShopCategory();
        wxShopCategory.setCategoryId(categoryId);
        wxShopCategory.setStatus(2);
        wxShopCategoryService.modifyStatus(wxShopCategory);
        return "redirect:/shop/category";
    }
}
